package com.CGFrame.struts.bean;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;

import com.CGFrame.struts.bean.ReportDemoBean.TaskPeriod;

public class GenerateWorkbookBean {
	public static HSSFWorkbook getWorkbook(List beans,Date beginDate,Date endDate){
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFCellStyle style=wb.createCellStyle();
		style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
		
		//提供不同任务状态的颜色
		HSSFCellStyle blueStyle=wb.createCellStyle();
		blueStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
//		blueStyle.setFillPattern(HSSFCellStyle.FINE_DOTS );
		blueStyle.setFillForegroundColor(new HSSFColor.LIGHT_BLUE().getIndex());
		HSSFCellStyle redStyle=wb.createCellStyle();
		redStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
//		redStyle.setFillPattern(HSSFCellStyle.FINE_DOTS );
		redStyle.setFillForegroundColor(new HSSFColor.RED().getIndex());
		HSSFCellStyle coralStyle=wb.createCellStyle();
		coralStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
//		coralStyle.setFillPattern(HSSFCellStyle.FINE_DOTS );
		coralStyle.setFillForegroundColor(new HSSFColor.CORAL().getIndex());
		HSSFCellStyle yellowStyle=wb.createCellStyle();
		yellowStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
//		yellowStyle.setFillPattern(HSSFCellStyle.FINE_DOTS );
		yellowStyle.setFillForegroundColor(new HSSFColor.YELLOW().getIndex());
		HSSFCellStyle greenStyle=wb.createCellStyle();
		greenStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
//		greenStyle.setFillPattern(HSSFCellStyle.FINE_DOTS );
		greenStyle.setFillForegroundColor(new HSSFColor.AQUA().getIndex());
		HSSFCellStyle darkGreenStyle=wb.createCellStyle();
		darkGreenStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
//		darkGreenStyle.setFillPattern(HSSFCellStyle.FINE_DOTS );
		darkGreenStyle.setFillForegroundColor(new HSSFColor.DARK_GREEN().getIndex());
		HSSFCellStyle pinkStyle=wb.createCellStyle();
		pinkStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
//		pinkStyle.setFillPattern(HSSFCellStyle.FINE_DOTS );
		pinkStyle.setFillForegroundColor(new HSSFColor.PINK().getIndex());
		HSSFCellStyle greyStyle=wb.createCellStyle();
		greyStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
//		greyStyle.setFillPattern(HSSFCellStyle.FINE_DOTS );
		greyStyle.setFillForegroundColor(new HSSFColor.GREY_50_PERCENT().getIndex());
		
		
		HSSFSheet sheet=wb.createSheet("员工工作表");
		
		//第一行,日期表头
		HSSFRow row=sheet.createRow(0);
		int length=ReportDemoBean.compareDay(beginDate, endDate);
		Calendar cal=Calendar.getInstance();
		cal.setTime(beginDate);
		//第一行第一格为空格
		row.createCell(0);
		for(int i=0;i<=length;i++){
			HSSFCell cell=row.createCell(i+1);
			cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			cell.setCellValue(cal.getTime());
			cell.setCellStyle(style);
			cal.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		
		int rowNum=1;
		int colNum;
		for(Iterator<ReportDemoBean> iter=beans.iterator();iter.hasNext();){
			ReportDemoBean bean=iter.next();
			HSSFRow vRow=sheet.createRow(rowNum);
			
			//第一列为人名
			HSSFCell nameCell=vRow.createCell(0);
			nameCell.setCellType(HSSFCell.CELL_TYPE_STRING);
			nameCell.setCellValue(new HSSFRichTextString(bean.getName()));
			
			List taskPeriods=bean.getTaskPeriods();
			for(Iterator iter2=taskPeriods.iterator();iter2.hasNext();){
				TaskPeriod tp=(TaskPeriod)iter2.next();
				colNum=tp.getBegin()+1;
				HSSFCell taskCell=vRow.createCell(colNum);
				taskCell.setCellType(HSSFCell.CELL_TYPE_STRING);
				taskCell.setCellValue(new HSSFRichTextString(tp.getTaskName()));
				sheet.addMergedRegion(new CellRangeAddress(rowNum,rowNum,colNum,(tp.getEnd()+1)));
				String color=tp.getColor();
				if(color.equals("HasInput")){
					taskCell.setCellStyle(greyStyle);
				}else if(color.equals("HasAssigned")){
					taskCell.setCellStyle(greenStyle);
				}else if(color.equals("IsDoing")){
					taskCell.setCellStyle(redStyle);
				}else if(color.equals("WaitingConfirm")){
					taskCell.setCellStyle(coralStyle);
				}else if(color.equals("ConfirmNotPassed")){
					taskCell.setCellStyle(darkGreenStyle);
				}else if(color.equals("ConfirmPassed")){
					taskCell.setCellStyle(pinkStyle);
				}else if(color.equals("Finished")){
					taskCell.setCellStyle(yellowStyle);
				}else if(color.equals("StandBy")){
					taskCell.setCellStyle(blueStyle);
				}
			}
			rowNum++;
		}
//		单元格自适应宽度		
		for(short i=0;i<length+2;i++){
			sheet.autoSizeColumn(i, true);
		}
		return wb;
	}
	
}
